var t = require("../../../api.js"), a = require("../../../area-picker/area-picker.js"), i = getApp();

Page({
    data: {},
    onLoad: function(e) {
        i.pageOnLoad(this, e);
        var o = this;
        o.getDistrictData(function(t) {
            a.init({
                page: o,
                data: t
            });
        }), wx.showLoading({
            title: "正在加载"
        }), i.request({
            url: t.mch.user.setting,
            success: function(t) {
                var data = t.data;
                data.send_type = t.data.mch ? t.data.mch.send_type:0;
                data.is_tz_peisong = t.data.mch ? t.data.mch.is_tz_peisong:0;
                data.is_rebate = t.data.mch ? t.data.mch.is_rebate:0;
                data.longitude = t.data.mch ? t.data.mch.longitude:0;
                data.latitude = t.data.mch ? t.data.mch.latitude:0;

                wx.hideLoading(), o.setData(t.data);
            }
        });
    },
    getDistrictData: function(a) {
        var e = wx.getStorageSync("district");
        if (!e) return wx.showLoading({
            title: "正在加载",
            mask: !0
        }), void i.request({
            url: t.default.district,
            success: function(t) {
                wx.hideLoading(), 0 == t.code && (e = t.data, wx.setStorageSync("district", e), 
                a(e));
            }
        });
        a(e);
    },
    onAreaPickerConfirm: function(t) {
        this.setData({
            edit_district: {
                province: {
                    id: t[0].id,
                    name: t[0].name
                },
                city: {
                    id: t[1].id,
                    name: t[1].name
                },
                district: {
                    id: t[2].id,
                    name: t[2].name
                }
            }
        });
    },
    mchCommonCatChange: function(t) {
        this.setData({
            mch_common_cat_index: t.detail.value
        });
    },
    formSubmit: function(a) {
        var e = this;
        wx.showLoading({
            title: "正在提交",
            mask: !0
        }), a.detail.value.form_id = a.detail.formId, a.detail.value.mch_common_cat_id = e.data.mch_common_cat_index ? e.data.mch_common_cat_list[e.data.mch_common_cat_index].id : e.data.mch && e.data.mch.mch_common_cat_id ? e.data.mch.mch_common_cat_id : "",a.detail.value.send_type=e.data.send_type, a.detail.value.is_tz_peisong=e.data.is_tz_peisong ,a.detail.value.is_rebate=e.data.is_rebate,a.detail.value.longitude=e.data.longitude,a.detail.value.latitude=e.data.latitude,
        i.request({
            url: t.mch.user.setting_submit,
            method: "post",
            data: a.detail.value,
            success: function(t) {
                wx.hideLoading(), 0 == t.code ? wx.showModal({
                    title: "提示",
                    content: t.msg,
                    showCancel: !1,
                    success: function(t) {
                        t.confirm && wx.navigateBack({
                            delta: 1
                        });
                    }
                }) : e.showToast({
                    title: t.msg
                });
            }
        });
    },
    onReady: function() {
        i.pageOnReady(this);
    },
    onShow: function() {
        i.pageOnShow(this);
    },
    onHide: function() {
        i.pageOnHide(this);
    },
    onUnload: function() {
        i.pageOnUnload(this);
    },
    uploadLogo: function() {
        var t = this;
        i.uploader.upload({
            start: function(t) {
                wx.showLoading({
                    title: "正在上传",
                    mask: !0
                });
            },
            success: function(a) {
                0 == a.code ? (t.data.mch.logo = a.data.url, t.setData({
                    mch: t.data.mch
                })) : t.showToast({
                    title: a.msg
                });
            },
            error: function(a) {
                t.showToast({
                    title: a
                });
            },
            complete: function() {
                wx.hideLoading();
            }
        });
    },
    uploadHeaderBg: function() {
        var t = this;
        i.uploader.upload({
            start: function(t) {
                wx.showLoading({
                    title: "正在上传",
                    mask: !0
                });
            },
            success: function(a) {
                0 == a.code ? (t.data.mch.header_bg = a.data.url, t.setData({
                    mch: t.data.mch
                })) : t.showToast({
                    title: a.msg
                });
            },
            error: function(a) {
                t.showToast({
                    title: a
                });
            },
            complete: function() {
                wx.hideLoading();
            }
        });
    },
    // 发货方式改变
    sendChange: function (t) {
        var send_type = t.detail.value;
        this.setData({
            send_type: send_type
        })
    },
    // 配送方式
    peisongChange: function (t) {
        var peisong = t.detail.value;
        this.setData({
            is_tz_peisong: peisong
        })
    },
    // 是否限制距离
    rebateChange: function (t) {
        var is_rebate = t.detail.value;
        this.setData({
            is_rebate: is_rebate
        })
    },
    getLocatoin: function () {
        var that = this,data={};
        wx.chooseLocation({
            success: function (t) {
                data.latitude=t.latitude
                data.longitude=t.longitude
                that.setData(data)
            },
            fail: function(){
                wx.getSetting({
                    success: function(o) {
                        o.authSetting["scope.userLocation"] ? 1 : getApp().getauth({
                            content: "需要获取您的地理位置授权，请到小程序设置中打开授权！",
                            cancel: !1,
                            success: function(a) {
                                a.authSetting["scope.userLocation"] && that.location(t);
                            }
                        });
                    }
                });
            }
        })
    },
    location: function(t) {
        var a = this, e = t.currentTarget.dataset.index, o = a.data.list;
        wx.openLocation({
            latitude: parseFloat(o[e].latitude),
            longitude: parseFloat(o[e].longitude),
            name: o[e].name,
            address: o[e].address
        });
    },
});